#include "XmlRW.h"

CXmlRW::CXmlRW(void)
{
    m_IsWrite=false;
    QDateTime dt = QDateTime::currentDateTime();
    m_DomDoccument = new QDomDocument;
    QString filename;
    QDir qdir;
    filename.append(qdir.currentPath());
    filename.append("/");
    filename.append(dt.toString("yyyyMMdd"));
    filename.append(".xml");
    m_file = new QFile(filename);
    if (!m_file->open(QIODevice::WriteOnly | QIODevice::Text))
    {
        printf("Creat %s Fail\n",filename.toStdString().c_str());
        return;
    }
    m_file->close();
    QFileInfo qinfo(filename);
    if (!qinfo.exists())
    {
        printf("Xml is NOT found\n");
        return;
    }
}
CXmlRW::CXmlRW(const char *filename)
{
    if (!m_IsWrite)
    {
        qDebug()<<"Do not Write Xml";
        return;
    }
    if (!CreateXMl(filename))
    {
        return;
    }
}
CXmlRW::~CXmlRW(void)
{
    delete m_file;
    m_file = NULL;
    delete m_DomDoccument;
    m_DomDoccument = NULL;
}
void CXmlRW::Save()
{
    if (!m_file->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
    {
        return;
    }
    QTextStream out(m_file);
    out.setCodec("UTF-8");
    m_DomDoccument->save(out, 4, QDomNode::EncodingFromTextStream);
    m_file->close();
}
bool CXmlRW::CreateXMl(const char* filename)
{
    m_DomDoccument = new QDomDocument;
    m_file = new QFile(QLatin1String(filename));
    if (!m_file->open(QIODevice::WriteOnly | QIODevice::Text))
    {
        printf("Creat %s Fail\n",filename);
        return FALSE;
    }
    m_file->close();
    QFileInfo qinfo(filename);
    if (qinfo.exists())
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}
void CXmlRW::setRoot(QString RootElementName, QStringList ElementAttrList,
                     QStringList AttrValueList)
{
    if (!m_file->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
    {
        return;
    }
    Root = m_DomDoccument->createElement(RootElementName);
    for (int ii = 0; ii < ElementAttrList.size(); ii++)
    {
        QDomAttr RootAttr = m_DomDoccument->createAttribute(ElementAttrList.at(ii));
        RootAttr.setValue(AttrValueList.at(ii));
        Root.setAttributeNode(RootAttr);
    }
    m_DomDoccument->appendChild(Root);
    m_CurrentNode = Root;
    QTextStream out(m_file);
    out.setCodec("UTF-8");
    m_DomDoccument->save(out, 4, QDomNode::EncodingFromTextStream);
    m_file->close();
}
void CXmlRW::AddChildNode(QString tagName, QStringList ElementAttrList,
                          QStringList AttrValueList)
{
    if (!m_file->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
    {
        return;
    }
    QDomElement element = m_DomDoccument->createElement(tagName);
    for (int ii = 0; ii < ElementAttrList.count(); ii++)
    {
        QDomAttr elementAttr = m_DomDoccument->createAttribute(ElementAttrList.at(ii));
        element.setAttributeNode(elementAttr);
        if (ii <= AttrValueList.count())
        {
            elementAttr.setValue(AttrValueList.at(ii));
        }
    }
    m_CurrentNode.appendChild(element);
    m_CurrentNode = element;
    QTextStream out(m_file);
    out.setCodec("UTF-8");
    m_DomDoccument->save(out, 4, QDomNode::EncodingFromTextStream);
    m_file->close();
}

void  CXmlRW::GetParentNode()
{
    m_CurrentNode = m_CurrentNode.parentNode();
    Save();
}
void CXmlRW::GetChildNode()
{
    m_CurrentNode = m_CurrentNode.firstChild();
    Save();
}
void CXmlRW::GetSblingNode()
{
    m_CurrentNode = m_CurrentNode.nextSibling();
    Save();
}
QDomNode CXmlRW::GetCurrentNode()
{
    return m_CurrentNode;
}
void CXmlRW::SetWriteXml(bool iswrite/* =false */)
{
    m_IsWrite=iswrite;
}
